package com.OnTimeTeam.OnTime;
import java.util.ArrayList;

import com.OnTimeTeam.OnTime.R;
import com.OnTimeTeam.OnTime.SmartAlarmFeatures;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class RouteDetailsList extends ArrayAdapter<Boolean> {

	private ArrayList<Boolean> items; //all alarms details
	private SmartAlarmFeatures af;
	private String duration, distance;
	private String departureTime;
	private boolean isLink;
	
	//CTOR - init' the array of AlarmFeatures and ApplicationContext object
	public RouteDetailsList(Context context, int textViewResourceId, ArrayList<Boolean> items, SmartAlarmFeatures af, String duration, String distance, boolean isLink) {
		super(context, textViewResourceId, items);
		this.items = items;
		this.af = af;
		this.duration = duration;
		this.distance = distance;
		this.isLink = isLink;
		calculateDepartureTime();
	}
	
	public View getView(final int index, View convertView, ViewGroup parent) {
		
		View view = convertView;
		LayoutInflater inflater = null;
		
		if (view == null) {
			inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.route_item, null);//set the view
		}
		
		TextView entry = (TextView)view.findViewById(R.id.route_header);
		TextView details = (TextView)view.findViewById(R.id.route_info);
		ImageView arrow = (ImageView)view.findViewById(R.id.arrow); 

		switch(index){
			case 0:
				entry.setText(getContext().getResources().getString(R.string.wakeUpTime));
				details.setText(af.getHour() + ":" + af.getMinute());
				if(isLink)
					arrow.setVisibility(View.INVISIBLE);
				else
					arrow.setVisibility(View.VISIBLE);
				break;
			case 1: 
				entry.setText(getContext().getResources().getString(R.string.departureTime));
				//departureTime = "Dep";
				details.setText(this.departureTime);	
				arrow.setVisibility(View.INVISIBLE);
				break;
			case 2:
				entry.setText(getContext().getResources().getString(R.string.arrivalTime));
				details.setText(af.getArrivalHour() + ":" + af.getArrivalMinute());
				arrow.setVisibility(View.INVISIBLE);
				break;
			case 3:
				if(af.getTransport().equals("Walk"))
					entry.setText(getContext().getResources().getString(R.string.routeDurationFoot));
				else if(af.getTransport().equals("Vehicle"))
					entry.setText(getContext().getResources().getString(R.string.routeDurationCar));
				else if(af.getTransport().equals("Transit"))
					entry.setText(getContext().getResources().getString(R.string.routeDurationTransit));
				else if(af.getTransport().equals("Bicycle"))
					entry.setText(getContext().getResources().getString(R.string.routeDurationBicycle));

				details.setText(duration.replace("mins", getContext().getResources().getString(R.string.minutes)));
				
				arrow.setVisibility(View.INVISIBLE);
				break;					
			case 4:
				entry.setText(getContext().getResources().getString(R.string.distance));
				details.setText(distance.replace("km", getContext().getResources().getString(R.string.km)));
				arrow.setVisibility(View.INVISIBLE);
				
				break;		
		}//end cases
		
		//return the view of the item
		return view;
	
	}//end of method 'getView'
	
	private void calculateDepartureTime(){
		
		String arrivalHour = af.getArrivalHour();
		String arrivalMinutes = af.getArrivalMinute();
		
		//duration = duration.replace("mins", getContext().getResources().getString(R.string.minutes));
		String durationHours = "0", durationMins = "0", durationTemp = "";
		
		
		if(arrivalHour == "00")
			arrivalHour = "24";
		//1 hours, 48 min
		//48 hours, min
		double arrivalNumeric = (Integer.parseInt(arrivalHour)*60) + Integer.parseInt(arrivalMinutes);		
		
		durationTemp = duration.substring(0, duration.lastIndexOf(" "));
		//duration is in format of X hours Y minutes
		if(durationTemp.contains(" "))
		{
			durationMins = durationTemp.substring(durationTemp.lastIndexOf(" ") +1, durationTemp.length());
			durationHours = durationTemp.substring(0, durationTemp.indexOf(" "));
		}
		//only minutes
		else if (duration.substring(duration.indexOf(" ") + 1, duration.length()).equalsIgnoreCase(getContext().getResources().getString(R.string.minutesLong)))	
			durationMins = duration.substring(0, duration.lastIndexOf(" "));			
		
		else //only hours
			durationHours = duration.substring(0, duration.lastIndexOf(" "));			
		
		System.out.println("#################### Hours: " + durationHours + " Minutes: " + durationMins + " #################");
		System.out.println("#################### Duration:" + duration + " #################");
		
		double departureTime = (arrivalNumeric - (double)((Integer.parseInt(durationHours) * 60) + (Integer.parseInt(durationMins))))/60;
		this.departureTime = String.valueOf(departureTime);
		
		String depHours = this.departureTime.substring(0, this.departureTime.indexOf("."));		
		String depMinutes = this.departureTime.substring(this.departureTime.indexOf(".") + 1, this.departureTime.length());
		
		depMinutes = "0." + depMinutes;
		depMinutes = String.valueOf((int) (Double.parseDouble(depMinutes) * 60));
		
		
		if(depHours.startsWith("-", 0))
		
		//	depHours = String.valueOf(Integer.parseInt(depHours) * (-1));
			depMinutes = String.valueOf(Integer.parseInt(depMinutes) * (-1));
			
		if(depHours.equals("-0"))
			depHours = "0";
		
		 if (Integer.parseInt(depHours) < 0 || Integer.parseInt(depMinutes) < 0 )
		{
			 int i_depHours = Integer.parseInt(depHours);
			 
			 if(depMinutes.equals("0.0"))
							
				 i_depHours += 24;
			 else
				 i_depHours += 23;
			 
			 
				depHours = String.valueOf(i_depHours);
		}
		 
		if(Integer.parseInt(depHours) < 10 && Integer.parseInt(depHours) >= 0 )
			depHours = "0" + depHours;
		
		if(Integer.parseInt(depHours) == 24)
			depHours = "00";
	
		
		if (Integer.parseInt(depMinutes) < 0)
		{
			int i_depMinutes = Integer.parseInt(depMinutes) + 60;
			depMinutes = String.valueOf(i_depMinutes);
		}
		
		if(Integer.parseInt(depMinutes) < 10 && Integer.parseInt(depMinutes) >= 0)
			depMinutes = "0" + depMinutes;
		
		this.departureTime = depHours + ":" + depMinutes;
		
	}
	
	
	
}
